package com.my12306.train.controller;

import com.my12306.common.enums.CodeEnum;
import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.model.entity.DecreaseEntity;
import com.my12306.train.model.entity.OrderEntity;
import com.my12306.train.model.entity.RateEntity;
import com.my12306.train.model.entity.ZoneEntity;
import com.my12306.train.model.vo.OrderPricesVo;
import com.my12306.train.model.vo.OrderVo;
import com.my12306.train.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


/**
 * 用户订单
 *
 * @author ËÕÄÁÏ¦
 * @email
 * @date 2020-07-24 20:58:07
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private CarriageService carriageService;

    @Autowired
    private RateService rateService;

    @Autowired
    private DecreaseService decreaseService;

    @Autowired
    private ZoneService zoneService;


    /**
     * 列表
     */
    @PostMapping("/list")
    public ResponseData list(@RequestBody PageVo pageVo) {
        PageUtils page = orderService.queryOrderPage(pageVo);
        return ResponseData.ok().put("page", page);
    }

    /**
     * 列表
     */
    @PostMapping("/usr/lst/page.html")
    public ResponseData pagelist(@RequestBody PageVo pageVo) {
        PageUtils page = orderService.queryUserOrderPage(pageVo);
        return ResponseData.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{orderId}")
    public ResponseData info(@PathVariable("orderId") Long orderId) {
        OrderEntity order = orderService.getById(orderId);

        return ResponseData.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResponseData save(@RequestBody OrderEntity order) {
        orderService.save(order);

        return ResponseData.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResponseData update(@RequestBody OrderEntity order) {
        orderService.updateById(order);

        return ResponseData.ok();
    }
    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public ResponseData delete(@RequestBody List<Long> orderIds) {
        System.out.println(orderIds);
        orderService.deleteOrderBatchByIds(orderIds);
        return ResponseData.ok();
    }

    @PostMapping("/sv/ticket.html")
    public ResponseData saveTicket(@RequestBody OrderVo orderVo){
        if (orderVo!=null){
           return this.orderService.saveTicket(orderVo);
        }
        return ResponseData.fail(CodeEnum.FAIL.getCode(),"你的订票已经被抢购完，请重新选择！");
    }
    @PostMapping("/prices")
    public ResponseData calPrices(@RequestBody OrderPricesVo orderPricesVo) {
        if (orderPricesVo == null || orderPricesVo.getCoachSitType() == null || orderPricesVo.getTotalDistance() == null) {
            return ResponseData.fail(400, "车厢id和总距离不能为空");
        }
        RateEntity rateEntity = rateService.findFirstByType(orderPricesVo.getCoachSitType());
        if (rateEntity == null) {
            rateEntity = rateService.findFirstTicketRateByRatio(100.0);
        }
        BigDecimal calPrices = BigDecimal.valueOf(0);
        List<DecreaseEntity> decreaseEntityList = decreaseService.queryAllDecreaseById(rateEntity.getId());
        if (decreaseEntityList == null) {
            return ResponseData.fail(400, "递增递减无该票价率");
        }

        Double endDistance = -1.0, nxDistance = 99999999.0;
        Double endRatio = -1.0, nxRatio = 99999999.0;
        double endToltalPrices = -1.0, nxToltaprices = 99999999.0;

        for (int i = decreaseEntityList.size() - 1; i >= 0; --i) {
            DecreaseEntity decreaseEntity = decreaseEntityList.get(i);
            ZoneEntity zoneEntity = zoneService.queryZoneById(decreaseEntity.getZoneId());
//            System.out.println("-------------------");
//            System.out.println(decreaseEntity);
//            System.out.println(zoneEntity);
//            System.out.println("-------------------");
            if (orderPricesVo.getTotalDistance() >= zoneEntity.getZoneTo() && zoneEntity.getZoneTo() >= endDistance) {
//                System.out.println("in=>>>>>>>>>>>>>>>>>> " + zoneEntity.getZoneTo());
                endDistance = zoneEntity.getZoneTo();
                endRatio = decreaseEntity.getTicketRate();
                endToltalPrices = decreaseEntity.getTotalPrice().doubleValue();
            }
        }

        nxRatio = endRatio;

        for (int i = decreaseEntityList.size() - 1; i >= 0; --i) {
            DecreaseEntity decreaseEntity = decreaseEntityList.get(i);
            ZoneEntity zoneEntity = zoneService.queryZoneById(decreaseEntity.getZoneId());
            if (zoneEntity.getZoneFrom() > endDistance && zoneEntity.getZoneTo() <= nxDistance) {
                    nxDistance = zoneEntity.getZoneTo();
                    nxRatio = decreaseEntity.getTicketRate();
                    nxToltaprices = decreaseEntity.getTotalPrice().doubleValue();
            }
        }
//        System.out.println("/////////////////////////////////////////");
        System.out.println(endToltalPrices + nxRatio * (orderPricesVo.getTotalDistance() - endDistance));
//        System.out.println("/////////////////////////////////////////");
        calPrices = calPrices.add(BigDecimal.valueOf(endToltalPrices + nxRatio * (orderPricesVo.getTotalDistance() - endDistance)));
        calPrices = calPrices.multiply(BigDecimal.valueOf(9.8));
//        System.out.println(orderPricesVo);
//        System.out.println(endDistance);
//        System.out.println(nxDistance);
//        System.out.println(calPrices.doubleValue());
//        System.out.println(endDistance + ' ' + endRatio + ' ' + endToltalPrices);
//        System.out.println(nxDistance + ' ' + nxRatio + ' ' + nxToltaprices);
        return ResponseData.ok().put("prices", calPrices.doubleValue());
    }
}
